<?php

class Xzsdsplus_Costpriceplus_Block_Grid extends Mage_Adminhtml_Block_Widget_Grid {

	public function __construct() {
		parent::__construct();
		$this->setId('banner_id');
		$this->setDefaultSort('banner_id');
		$this->setDefaultDir('desc');
		$this->setSaveParametersInSession(true);
		$this->setUseAjax(true);
		$this->setVarNameFilter('banner_filter');
	}

	protected function _getStore() {
		$storeId = (int) $this->getRequest()->getParam('store', 0);
		return Mage::app()->getStore($storeId);
	}

	protected function _prepareCollection() {
		$store = $this->_getStore();
		$collection = Mage::getModel('xbanner/banner')->getCollection()
			->addAttributeToSelect('banner_id')
			->addAttributeToSelect('title')
			->addAttributeToSelect('start_at')
			->addAttributeToSelect('end_at');

		$this->setCollection($collection);

		parent::_prepareCollection();
		return $this;
	}

	protected function _prepareColumns() {
		
		$this->addColumn('banner_id',
			array(
				'header'=> Mage::helper('xbanner')->__('ID'),
				'width' => '50px',
				'type'  => 'number',
				'index' => 'banner_id',
		));
		
		$this->addColumn('title',
			array(
				'header'=> Mage::helper('xbanner')->__('Title'),
				'index' => 'title',
		));

		$this->addColumn('start_at', array(
			'header'    =>  Mage::helper('xbanner')->__('Banner Start'),
			'type'      =>	'datetime',
			'index'     =>	'start_at',
			'gmtoffset' => true,
			'default'	=> 	' ---- '
		));
		
		$this->addColumn('end_at', array(
			'header'    =>  Mage::helper('xbanner')->__('Banner End'),
			'type'      =>	'datetime',
			'index'     =>	'end_at',
			'gmtoffset' => true,
			'default'	=> 	' ---- '
		));
		
		$this->addColumn('status',
			array(
				'header'=> Mage::helper('xbanner')->__('Status'),
				'width' => '70px',
				'index' => 'status',
				'type'  => 'options',
				'options' => Mage::getSingleton('xbanner/status')->getOptionArray(),
		));
		
		$this->addColumn('action',
			array(
				'header'    => Mage::helper('xbanner')->__('Action'),
				'width'     => '50px',
				'type'      => 'action',
				'getter'     => 'getId',
				'actions'   => array(
					array(
						'caption' => Mage::helper('xbanner')->__('Edit'),
						'url'     => array(
							'base'=>'*/*/edit',
							'params'=>array('store'=>$this->getRequest()->getParam('store'))
						),
						'field'   => 'id'
					)
				),
				'filter'    => false,
				'sortable'  => false,
				'index'     => 'stores',
		));

		return parent::_prepareColumns();
	}
	
	protected function _prepareMassaction() {
		
		$this->setMassactionIdField('banner_id');
		$this->getMassactionBlock()->setFormFieldName('banner');

		$this->getMassactionBlock()->addItem('delete', array(
			 'label'=> Mage::helper('xbanner')->__('Delete'),
			 'url'  => $this->getUrl('*/*/massDelete'),
			 'confirm' => Mage::helper('xbanner')->__('Are you sure?')
		));

		$statuses = Mage::getSingleton('xbanner/status')->getOptionArray();

		array_unshift($statuses, array('label'=>'', 'value'=>''));
		$this->getMassactionBlock()->addItem('status', array(
			 'label'=> Mage::helper('xbanner')->__('Change status'),
			 'url'  => $this->getUrl('*/*/massStatus', array('_current'=>true)),
			 'additional' => array(
					'visibility' => array(
						 'name' => 'status',
						 'type' => 'select',
						 'class' => 'required-entry',
						 'label' => Mage::helper('xbanner')->__('Status'),
						 'values' => $statuses
					 )
			 )
		));

		return $this;
	}

	public function getGridUrl() {
		return $this->getUrl('*/*/grid', array('_current'=>true));
	}

	public function getRowUrl($row) {
		return $this->getUrl('*/*/edit', array(
			'store'=>$this->getRequest()->getParam('store'),
			'id'=>$row->getId())
		);
	}
}
